import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import WelCome from '../views/WelCome.vue'
import users from '../views/users.vue'
import product from '../views/product.vue'
import producttype from '../views/producttype.vue'
import userss from '../views/userss.vue'
import rote from '../views/rote.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect:'/home/welcome',
    children:[
      {path:"welcome",component:WelCome},
      {path:"users",component:users},
      {path:"product",component:product},
      {path:"producttype",component:producttype},
      {path:"userss",component:userss},
      {path:"rote",component:rote}

    ]
  },
  {
    path:'/home',
    name:'home',
    component: Home,
    redirect:'/home/welcome',
    children:[
      {path:"welcome",component:WelCome},
      {path:"users",component:users},
      {path:"product",component:product},
      {path:"producttype",component:producttype},
      {path:"userss",component:userss},
      {path:"rote",component:rote}
    ]
  },
  {
    path: '/about',
    name: 'About',
    component: () => import('../views/About.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/Login.vue')
  },
  {
    path:'/welcome',
    name:'welcome',
    component:()=>import('../views/WelCome.vue')
  }
]


const router = new VueRouter({
  routes
})
router.beforeEach((to,from,next)=>{
  if(to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()
})
export default router
